package edu.zjut.hjg.saas.biz.service.impl;

import edu.zjut.hjg.saas.biz.dao.NewsDao;
import edu.zjut.hjg.saas.biz.dao.UserLogDao;
import edu.zjut.hjg.saas.biz.model.News;
import edu.zjut.hjg.saas.biz.model.UserLog;
import edu.zjut.hjg.saas.biz.service.NewsService;
import edu.zjut.hjg.saas.biz.support.OperateType;
import edu.zjut.hjg.saas.biz.support.Paginated;

public class NewsServiceImpl implements NewsService {

    private NewsDao    newsDao;
    private UserLogDao userLogDao;

    public void setNewsDao(NewsDao newsDao) {
        this.newsDao = newsDao;
    }

    public void setUserLogDao(UserLogDao userLogDao) {
        this.userLogDao = userLogDao;
    }

    @Override
    public Integer create(News news, String operator) {
        Integer id = newsDao.create(news);
        UserLog userLog = new UserLog();
        userLog.setKindergarten(news.getKindergarten());
        userLog.setLoginId(operator);
        userLog.setOperateType(OperateType.INSERT);
        userLog.setDescription("添加了一条新闻动态记录");
        userLogDao.create(userLog);
        return id;
    }

    @Override
    public News findByIdAndAppId(int id, int appId) {
        News news = newsDao.findByIdAndAppId(id, appId);
        if (news == null) {
            throw new RuntimeException("no news found for id=" + id + " and appId=" + appId);
        }
        return news;
    }

    @Override
    public Paginated<News> listByAppId(int appId, int pageSize, int page) {
        return newsDao.listByAppId(appId, pageSize, page);
    }
}
